package com.sinosoft.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;


/**
 * @author loujiang
 * @date 2021/1/2 10:20 上午
 * @NOTE:
 */
@Slf4j
@Component
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {


        return ((exchange, chain) ->
        {
            log.info("请求近来了 a={} b={}", config.getName(), config.getValue());
            ServerHttpRequest modifiedRequest = exchange.getRequest().mutate().build();
            ServerWebExchange serverWebExchange =
                    exchange.mutate().request((ServerHttpRequest) modifiedRequest).build();

            return chain.filter(serverWebExchange);
        });
    }
}
